package com.enterprise.viewer.client.view;

import com.enterprise.viewer.client.presenter.GadgetPresenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.FrameElement;
import com.google.gwt.dom.client.StyleElement;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class Gadget extends Composite implements GadgetPresenter.Display {

	private static GadgetUiBinder uiBinder = GWT.create(GadgetUiBinder.class);

	interface GadgetUiBinder extends UiBinder<Widget, Gadget> {
	}

	@UiField HTMLPanel container;
	@UiField HorizontalPanel headerbar; 
	@UiField Label label;
	@UiField HorizontalPanel buttonbar;
	@UiField  (provided=true) Frame frame;
	//http://comments.gmane.org/gmane.org.google.gwt/73550

	public Gadget() {
		frame = new Frame();
		/*frame.setVisible(true);
		frame.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {FrameElement frameElement = frame.getElement().cast();
			addStyleSheet(frameElement, "div {background: #ff0000; margin-right:5%;}");
			}
		});*/
		
		initWidget(uiBinder.createAndBindUi(this));
		


		//String s = "<iframe src='http://" + url + "' height='100%' width='100%' seamless='true' name='myframe' scrolling='no' frameborder='0'></iframe>";

	}
	
	@Override
	protected void onLoad() {
		super.onLoad();
		//frame.setUrl("http://www.google.com");
		//FrameElement frameElement = frame.getElement().cast();
		//addStyleSheet(frameElement, "div {background: #ff0000; margin-right:5%;}");
	}

	public static StyleElement addStyleSheet(FrameElement frameElement,	String cssText) {

		Document contentDocument = frameElement.getContentDocument();
		Element targetElement = contentDocument.getElementsByTagName("head")
				.getItem(0);

		if(targetElement == null){
			targetElement = contentDocument.getDocumentElement()
					.getFirstChildElement();

			if(targetElement == null){
				contentDocument.insertFirst(targetElement = contentDocument
						.createElement("head"));
			}
		}

		StyleElement styleElement = contentDocument.createStyleElement();
		styleElement.setType("text/css");
		styleElement.setCssText(cssText);

		targetElement.insertFirst(styleElement);

		return styleElement;
	}

	@Override
	public HorizontalPanel getHeaderBar() {
		return headerbar;
	}

	@Override
	public HorizontalPanel getButtonBar() {
		return buttonbar;
	}

	@Override
	public Frame getFrame() {
		return frame;
	}

	@Override 
	public Label getLabel(){
		return label;
	}

	@Override
	public void setURL(String url) {
		frame.setUrl(url);
	}
}
